<?php

namespace app\common\service;

use Db;
use Session;

class  RbacSrv{







    /*
     * 获取制定用户的权限
     */
    public function getUserAccess($uid){

        $access = Db::table('qi_role_user')->alias('a')
            ->join('qi_role_menu b', 'a.role_id = b.role_id')
            ->where('a.user_id', $uid)
            ->select();
        $menuIdData = [];

        foreach($access as $item){
            $menuIdData[] = $item['menu_id'];
        }
        $menuIdData = array_merge(array_unique($menuIdData));

        if(empty($menuIdData)){
            return [];
        }

        $menuData = Db::table('qi_menu')->where('id', 'IN', $menuIdData)->select();
        $menuLink = [];

        foreach($menuData as $item){
            if(!empty($item['link'])){
                $menuLink[] = $item['link'];
            }
        }

        Session::set('userAccess', $menuLink);
    }


    /*
     * 验证权限
     */
    public function checkPower($url){
        $hasAccess = Session::get('userAccess');

        foreach ($hasAccess as $item){
            if(strpos($url, $item) !== false ){
                return true;
            }
        }

        return false;

    }



}
